{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Rectangle Area"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #geometry #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #几何 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: computeArea"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #矩形面积"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你 <strong>二维</strong> 平面上两个 <strong>由直线构成且边与坐标轴平行/垂直</strong> 的矩形，请你计算并返回两个矩形覆盖的总面积。</p>\n",
    "\n",
    "<p>每个矩形由其 <strong>左下</strong> 顶点和 <strong>右上</strong> 顶点坐标表示：</p>\n",
    "\n",
    "<div class=\"MachineTrans-Lines\">\n",
    "<ul>\n",
    "\t<li class=\"MachineTrans-lang-zh-CN\">第一个矩形由其左下顶点 <code>(ax1, ay1)</code> 和右上顶点 <code>(ax2, ay2)</code> 定义。</li>\n",
    "\t<li class=\"MachineTrans-lang-zh-CN\">第二个矩形由其左下顶点 <code>(bx1, by1)</code> 和右上顶点 <code>(bx2, by2)</code> 定义。</li>\n",
    "</ul>\n",
    "</div>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"Rectangle Area\" src=\"https://assets.leetcode.com/uploads/2021/05/08/rectangle-plane.png\" style=\"width: 700px; height: 365px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2\n",
    "<strong>输出：</strong>45\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2\n",
    "<strong>输出：</strong>16\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>-10<sup>4</sup> &lt;= ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 &lt;= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [rectangle-area](https://leetcode.cn/problems/rectangle-area/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [rectangle-area](https://leetcode.cn/problems/rectangle-area/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['-3\\n0\\n3\\n4\\n0\\n-1\\n9\\n2', '-2\\n-2\\n2\\n2\\n-2\\n-2\\n2\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n",
    "        def area(x1, y1, x2, y2):\n",
    "            return (x1 - x2) * (y1 - y2)\n",
    "        ans = area(ax1, ay1, ax2, ay2) + area(bx1, by1, bx2, by2)\n",
    "        ix1 = max(ax1, bx1)\n",
    "        iy1 = max(ay1, by1)\n",
    "        ix2 = min(ax2, bx2)\n",
    "        iy2 = min(ay2, by2)\n",
    "        if ix2 > ix1 and iy2 > iy1:\n",
    "            ans -= area(ix1, iy1, ix2, iy2)\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A, B, C, D, E, F, G, H):\n",
    "        \"\"\"\n",
    "        :type A: int\n",
    "        :type B: int\n",
    "        :type C: int\n",
    "        :type D: int\n",
    "        :type E: int\n",
    "        :type F: int\n",
    "        :type G: int\n",
    "        :type H: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        area1, area2 = (C - A) * (D - B), (G - E) * (H - F)\n",
    "        _long, _width = min(C, G) - max(A, E), min(D, H) - max(B, F)\n",
    "        overlap = _long * _width if _long > 0 and _width > 0 else 0\n",
    "        return area1 + area2 - overlap\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A, B, C, D,  E, F, G, H):\n",
    "        \"\"\"\n",
    "        :type A: int\n",
    "        :type B: int\n",
    "        :type C: int\n",
    "        :type D: int\n",
    "        :type E: int\n",
    "        :type F: int\n",
    "        :type G: int\n",
    "        :type H: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        \n",
    "        s = (C-A)*(D-B) + (G-E)*(H-F)\n",
    "        if E>=C or G<=A or H<=B or F>=D:\n",
    "            return s\n",
    "        print(s)\n",
    "        \n",
    "        return s - min(C-A, G-E,C-E, G-A) * min(H-F,D-B, H-B, D-F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A, B, C, D, E, F, G, H):\n",
    "        \"\"\"\n",
    "        :type A: int\n",
    "        :type B: int\n",
    "        :type C: int\n",
    "        :type D: int\n",
    "        :type E: int\n",
    "        :type F: int\n",
    "        :type G: int\n",
    "        :type H: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if B>=H or E>=C or F>=D or A>=G:\n",
    "            return (C-A) * (D-B) + (G-E) * (H-F)\n",
    "        width = min(C,G) - max(A,E)\n",
    "        height = min(D,H) - max(B,F)\n",
    "        return (C-A) * (D-B) + (G-E) * (H-F) - width * height\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "        s=abs((D-B)*(C-A))+abs((H-F)*(G-E))\n",
    "        m,n=0,0\n",
    "        if (D>=H and B<=H) or (D<=H and D>=F):\n",
    "            m=min(D,H)-max(B,F)\n",
    "        if (C>=E and C<=G) or (G<=C and G>=A):\n",
    "            n=min(C,G)-max(A,E)\n",
    "        return s-n*m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "        # AC EG求重叠\n",
    "        def func(x1, x2, x3, x4):\n",
    "            if x1 <= x3 < x4 <= x2:\n",
    "                return x4 - x3\n",
    "            elif x3 <= x1 < x2 <= x4:\n",
    "                return x2 - x1\n",
    "            elif x1 <= x3 < x2 <= x4:\n",
    "                return x2 - x3\n",
    "            elif x3 <= x1 < x4 <= x2:\n",
    "                return x4 - x1\n",
    "            else:\n",
    "                return -1\n",
    "        a = func(A, C, E, G)\n",
    "        b = func(F, H, B, D)\n",
    "        if a == -1 or b == -1:\n",
    "            return (D - B) * (C - A) + (H - F) * (G - E)\n",
    "        return (D - B) * (C - A) + (H - F) * (G - E) - a * b\n",
    "        # print(a, b)\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "        return (C - A) * (D - B) + (G - E) * (H - F) - max(0, min(C, G) - max(A, E)) * max(0, min(D, H) - max(B, F))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "#         return (C-A)*(D-B) + (H-F)*(G-E)-max(0,min(C,G)-max(A,E))*max(0,min(D,H)-max(B,F))\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "        w = min(C, G) - max(A, E)\n",
    "        h = min(D, H) - max(B, F)\n",
    "        m = (C - A) * (D - B) + (G - E) * (H - F)\n",
    "        if w < 0 or h < 0:\n",
    "            return m\n",
    "        return m - w * h\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "        if B >= H or D <= F or C <= E or A >=G :\n",
    "            return (C-A)*(D-B) + (G-E)*(H-F)\n",
    "        Xs = sorted([A,C,E,G])\n",
    "        Ys = sorted([B,D,F,H])\n",
    "        return (C-A)*(D-B) + (G-E)*(H-F) - ((Xs[2] - Xs[1])*(Ys[2] - Ys[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "        total = (C-A) * (D-B) + (G-E)*(H-F)\n",
    "        if A >= G or E>=C or B>=H or F>=D:\n",
    "            return total\n",
    "\n",
    "        left = max(A, E)\n",
    "        right = min(G, C)\n",
    "        top = min(D, H)\n",
    "        down = max(B, F)\n",
    "         \n",
    "        \n",
    "        return total - (right - left) * (top - down)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "        r12 = (C-A)*(D-B) + (G-E)*(H-F)\n",
    "        if A > G or C < E or D < F or B > H:#基于边界的考虑\n",
    "            return r12\n",
    "        else:\n",
    "            x1 = max(A, E)#只要是在右边都是取最小的那个，在右边取最大的那个，然后右边减去左边，就是\n",
    "            #重合的面积了\n",
    "            x2 = min(C, G)\n",
    "            y1 = max(B, F)\n",
    "            y2 = min(D, H)\n",
    "            return r12 - (y2-y1)*(x2-x1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "        lx = max(A, E) # 重叠区域左边界的x\n",
    "        by = max(B, F) # 重叠区域下边界的y\n",
    "        rx = min(C, G) # 重叠区域右边界的x\n",
    "        ty = min(D, H) # 重叠区域上边界的y\n",
    "\n",
    "        area1 = (C - A)*(D - B)\n",
    "        area2 = (G - E)*(H - F)\n",
    "        if E > C or A > G or F > D or H < B:\n",
    "            return area1 + area2\n",
    "        return area1 + area2 - (rx - lx)*(ty - by)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "        if A>G or C<E or B>H or D<F:\n",
    "            return (C-A)*(D-B)+(G-E)*(H-F)\n",
    "        top=min(D,H)\n",
    "        left=max(A,E)\n",
    "        right=min(C,G)\n",
    "        bottom=max(B,F)\n",
    "        print(top,left,right,bottom)\n",
    "\n",
    "        return (C-A)*(D-B)+(G-E)*(H-F)-(bottom-top)*(left-right)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    @staticmethod\n",
    "    def compare(a, b, c):\n",
    "        if a >= b and a <=c:\n",
    "            return True\n",
    "\n",
    "    def find_min_length(self, A, B, C, D):\n",
    "        if self.compare(C, A, B):\n",
    "            if self.compare(D, A, B):\n",
    "                return (D - C)\n",
    "            else:\n",
    "                return (B - C)\n",
    "        if self.compare(D, A, B):\n",
    "            return (D - A)\n",
    "        if (self.compare(A, C , D)) and (self.compare(B, C, D)):\n",
    "            return B - A\n",
    "        return 0\n",
    "\n",
    "    def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:\n",
    "        width = self.find_min_length(A, C, E, G)\n",
    "        height = self.find_min_length(B, D, F, H)\n",
    "        area_size = (width * height)\n",
    "        print(width, height)\n",
    "        print(area_size)\n",
    "        return ((C - A) * (D - B) + (G - E) * (H - F) - area_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n",
    "        a1 = abs((ax2 - ax1)*(ay2 - ay1))\n",
    "        a2 = abs((bx2 - bx1)*(by2 - by1))\n",
    "\n",
    "        lx, ly = 0, 0\n",
    "\n",
    "        if not (ax1 > bx2 or bx1 > ax2):\n",
    "            lx = min(ax2, bx2) - max(ax1, bx1)\n",
    "\n",
    "        if not (ay1 > by2 or by1 > ay2):\n",
    "            ly = min(ay2, by2) - max(ay1, by1)\n",
    "        \n",
    "        a3 = abs(lx * ly)\n",
    "        return a1 + a2 - a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n",
    "        len1 = min([ax2,bx2]) - max([ax1,bx1])\n",
    "        len2 = min([ay2,by2]) - max([ay1,by1])\n",
    "        overlap = len1 * len2\n",
    "        if len1 < 0 or len2 < 0:\n",
    "            overlap = 0\n",
    "        return (ax2-ax1)*(ay2-ay1) + (bx2-bx1)*(by2-by1) - overlap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\r\n",
    "        lx=max(ax1,bx1)\r\n",
    "        rx=min(ax2,bx2)\r\n",
    "        ly=max(ay1,by1)\r\n",
    "        ry=min(ay2,by2)\r\n",
    "        tot=(ax2-ax1)*(ay2-ay1)+(bx2-bx1)*(by2-by1)\r\n",
    "        return tot-max(0,rx-lx)*(max(0,ry-ly))\r\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n",
    "        s1 = abs(ax2-ax1) * abs(ay1-ay2)\n",
    "        s2 = abs(bx2-bx1) * abs(by1-by2)\n",
    "\n",
    "        w = min(ax2,bx2) - max(ax1, bx1)\n",
    "        h = min(ay2,by2) - max(ay1, by1)\n",
    "\n",
    "\n",
    "        return s1 + s2 - max(w, 0) * max(h,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n",
    "    \n",
    "        area1 = abs(ax1-ax2)*abs(ay1-ay2)\n",
    "        area2 = abs(bx1-bx2)*abs(by1-by2)\n",
    "\n",
    "        # 不能单纯的排序。必须要知道坐标代表哪个矩形\n",
    "        # xset = sorted([ax1,ax2,bx1,bx2])\n",
    "        # yset = sorted([ay1,ay2,by1,by2])\n",
    "        # overlap_x1 = xset[1:-1]\n",
    "        # overlap_y1 = yset[1:-1]\n",
    "\n",
    "        # overlap = min_ - max_两个矩形的左边\n",
    "        overlap_x= [max(ax1, bx1),min(ax2, bx2)]\n",
    "        overlap_y = [max(ay1, by1),min(ay2, by2)]\n",
    "        overlap_area = max(overlap_x[1]-overlap_x[0],0)*max(overlap_y[1]-overlap_y[0],0)\n",
    "        return area1 + area2 - overlap_area\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n",
    "        def cross(x,y,bx1,bx2,by1,by2):\n",
    "            if (x-bx1)*(x-bx2)<=0 and (y-by1)*(y-by2)<=0:\n",
    "                return True\n",
    "            return False\n",
    "        def two(a,bx1,bx2,by1,by2,ax1,ax2,ay1,ay2):\n",
    "            if a[0][1]==a[1][1]:#纵坐标\n",
    "                if (by1-ay1)*(by1-ay2)<0:\n",
    "                    return abs(ax1-ax2)*abs(a[0][1]-by1)\n",
    "                elif (by2-ay1)*(by2-ay2)<0:return abs(ax1-ax2)*abs(a[0][1]-by2) \n",
    "                else: return 0               \n",
    "            else: #横坐标\n",
    "                if (bx1-ax1)*(bx1-ax2)<0:\n",
    "                    return abs(ay1-ay2)*abs(a[0][0]-bx1)\n",
    "                elif (bx2-ax1)*(bx2-ax2)<0:return abs(ay1-ay2)*abs(a[0][0]-bx2) \n",
    "                else:return 0\n",
    "        def one(ax1,ax2,ay1,ay2,bx1,bx2,by1,by2):\n",
    "            if (ay1-by1)*(ay1-by2)<0 and (ay2-by1)*(ay2-by2)<0 and (bx1-ax1)*(bx1-ax2)<0 and (bx2-ax1)*(bx2-ax2)<0:\n",
    "                return abs(ay1-ay2)*abs(bx1-bx2)\n",
    "            if (by1-ay1)*(by1-ay2)<0 and (by2-ay1)*(by2-ay2)<0 and (ax1-bx1)*(ax1-bx2)<0 and (ax2-bx1)*(ax2-bx2)<0:\n",
    "                return abs(by1-by2)*abs(ax1-ax2)\n",
    "            return 0\n",
    "\n",
    "\n",
    "\n",
    "        point1=[(ax1,ay1),(ax1,ay2),(ax2,ay1),(ax2,ay2)]\n",
    "        point2=[(bx1,by1),(bx1,by2),(bx2,by1),(bx2,by2)]\n",
    "        a=[]\n",
    "        b=[]\n",
    "        for mem in point1:\n",
    "            if cross(mem[0],mem[1],bx1,bx2,by1,by2):\n",
    "                a.append(mem)\n",
    "        for mem in point2:\n",
    "            if cross(mem[0],mem[1],ax1,ax2,ay1,ay2):\n",
    "                b.append(mem)\n",
    "        #print(a,b)\n",
    "        l1,l2=len(a),len(b)\n",
    "        if l1==4:return abs(bx1-bx2)*abs(by1-by2)\n",
    "        if l2==4:return abs(ax1-ax2)*abs(ay1-ay2)\n",
    "        if not a and not b:return abs(ax1-ax2)*abs(ay1-ay2)+abs(bx1-bx2)*abs(by1-by2)-one(ax1,ax2,ay1,ay2,bx1,bx2,by1,by2)\n",
    "        if l1==1 and l2==1:return abs(ax1-ax2)*abs(ay1-ay2)+abs(bx1-bx2)*abs(by1-by2)-abs(a[0][0]-b[0][0])*abs(a[0][1]-b[0][1])\n",
    "        if l1==2:\n",
    "            return abs(ax1-ax2)*abs(ay1-ay2)+abs(bx1-bx2)*abs(by1-by2)-two(a,bx1,bx2,by1,by2,ax1,ax2,ay1,ay2)\n",
    "        if l2==2:\n",
    "            return abs(ax1-ax2)*abs(ay1-ay2)+abs(bx1-bx2)*abs(by1-by2)-two(b,ax1,ax2,ay1,ay2,bx1,bx2,by1,by2)\n",
    "        #if len(a)==2 and not b:return abs(ax1-ax2)*abs(ay1-ay2)+abs(bx1-bx2)*abs(by1-by2)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n",
    "        \n",
    "        return (ax2-ax1)*(ay2-ay1)+(bx2-bx1)*(by2-by1)-max(0,(min(ax2,bx2)-max(ax1,bx1)))*max(0,(min(ay2,by2)-max(ay1,by1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n",
    "        a1 = (ax2-ax1)*(ay2-ay1)\n",
    "        a2 = (bx2-bx1)*(by2-by1)\n",
    "        overlap_width = min(ax2,bx2) - max(ax1,bx1)\n",
    "        overlap_height = min(ay2,by2) - max(ay1,by1)\n",
    "        overlap_area = max(0, overlap_width) * max(0, overlap_height)\n",
    "        return a1+a2 - overlap_area"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n",
    "        area1 = (ax2 - ax1) * (ay2 - ay1)\n",
    "        area2 = (bx2 - bx1) * (by2 - by1)\n",
    "        overlapw = min(ax2, bx2) - max(ax1, bx1)\n",
    "        overlapy = min(ay2, by2) - max(ay1, by1)\n",
    "        overlapa = max(overlapw, 0)*max(overlapy, 0)\n",
    "        return area1+area2-overlapa"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
